package com.example.chachacha15;

import java.net.URL;
import java.util.Arrays;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class HelloController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField arrTF;

    @FXML
    private Button clear;

    @FXML
    private Label resLabel;

    @FXML
    private Button solve;

    @FXML
    void clearClick(ActionEvent event) {
        arrTF.setText("");
    }

    @FXML
    void solveClick(ActionEvent event) {
        int[] arr = Arrays.stream(arrTF.getText().split(",")).mapToInt(Integer::parseInt).toArray();
        int n = arr.length;
        int maxIndex = 0, minIndex = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i] > arr[maxIndex]) {
                maxIndex = i;
            }
            if (arr[i] < arr[minIndex]) {
                minIndex = i;
            }
        }

        int resultIndex = -1;
        for (int i = 0; i < n; i++) {
            if (i == maxIndex || i == minIndex) {
                continue;
            }
            if (i == maxIndex - 1 || i == maxIndex + 1 || i == minIndex - 1 || i == minIndex + 1) {
                if (resultIndex == -1 || arr[i] > arr[resultIndex]) {
                    resultIndex = i;
                }
            }
        }

        resLabel.setText(Integer.toString(resultIndex));
    }

    @FXML
    void initialize() {
        assert arrTF != null : "fx:id=\"arrTF\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert clear != null : "fx:id=\"clear\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert resLabel != null : "fx:id=\"resLabel\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert solve != null : "fx:id=\"solve\" was not injected: check your FXML file 'hello-view.fxml'.";

    }

}